package com.zhangyj.impl.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.zhangyj.db.DBUtil;
import com.zhangyj.entitis.BSort;



public class DaoProSort {

	public List<BSort> getSortList() throws Exception {
		List<BSort> list = new ArrayList<BSort>();
		Connection dbc = null;
		ResultSet rs = null;
		try {
			String sql = "select cplh,cplm from producefl order by cplh";
			dbc = DBUtil.getConnection();
			PreparedStatement ps = dbc.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				BSort sort = new BSort();
				sort.setSortId(rs.getString(1));
				sort.setSortName(rs.getString(2));
				list.add(sort);
			}
			rs.close();
			ps.close();
		} catch (Exception e) {
			throw e;
		} finally {
			if (dbc != null)
				dbc.close();
		}
		return list;
	}
	
	public BSort getSortCon(String sortId) throws Exception {
		Connection dbc = null;
		ResultSet rs = null;
		BSort sort = new BSort();
		try {
			String sql = "select cplh,cplm from producefl where cplh = ?";
			dbc = DBUtil.getConnection();
			PreparedStatement ps = dbc.prepareStatement(sql);
			ps.setString(1, sortId);
			rs = ps.executeQuery();
			while (rs.next()) {
				sort.setSortId(rs.getString(1));
				sort.setSortName(rs.getString(2));
			}
		} catch (Exception e) {
			throw e;
		} finally {
			if (dbc != null)
				dbc.close();
		}
		return sort;
	}
	
	public void saveMod(String sortId, String sortName) throws Exception {
		java.sql.Connection dbc = null;
		try {
			String sql = "update prosort set sortname = ? where sortid = ?";
			dbc = DBUtil.getConnection();
			PreparedStatement ps = dbc.prepareStatement(sql);
			ps.setString(1, sortName);
			ps.setString(2, sortId);
			ps.executeUpdate();
			ps.close();
		} catch (Exception e) {
			throw e;
		} finally {
			if (dbc != null)
				dbc.close();
		}
	}

}
